<?php
/*
 * @author banban
* @datetime 2020-8-24 19:51:49
* @copyright 板板<249366615@qq.com>
* @filename Guid
*/
namespace App\Service\AppService;

class Sign
{
    private static $self=null;
    private $account='';//账号
    private $password='';//密码
    private $customscode='';//企业海关注册代码
    private $ebpCode='';//电商平台的海关注册登记编号
    private $payCode='';//支付企业的海关注册登记编号
    private function __construct($account,$password,$customscode,$ebpCode,$payCode){
        $this->account=$account;
        $this->password=$password;
        $this->customscode=$customscode;
        $this->ebpCode=$ebpCode;
        $this->payCode=$payCode;
    }
    public static function getInstance($account,$password,$customscode,$ebpCode,$payCode){
        if(!(self::$self instanceof self)){
            self::$self = new self($account,$password,$customscode,$ebpCode,$payCode);
        }
        return self::$self;
    }
    public function sign($data,$ordersn,$keyinfo=''){
        $res = array();
        $res['ceb:CEBSignMessage'] = array(
            'version'      =>      '1.0',
            'xmlns:ceb'    =>      'http://www.chinaport.gov.cn/ceb',
            'xmlns:xsi'    =>      'http://www.w3.org/2001/XMLSchema-instance',
            'ceb:SignData'   =>      array(
                'ceb:userInfo'   => strtoupper(md5($this->account.strtoupper(MD5($this->password)))),
                'ceb:customsRegistCode'   =>      $this->customscode,
                'ceb:waitSignData'   => base64_encode($data) ,
                'ceb:purpose'     => $this->ebpCode,
                'ceb:copNo'      => $ordersn,
                'ceb:note'      => $this->payCode
            )
        );
        if($keyinfo){
            $res['ceb:CEBSignMessage']['ceb:Authentication']=['ceb:keyInfo'=>$keyinfo];
        }
        $xml = new ArrayToXML;
        $res = $xml->toXml($res);
        return $res;
    }
    //海关生成唯一guid值
    public function getGuidOnlyValue(){
        $a4 = uniqid().rand(10,99);
        $a4 = $this->insertToStr($a4,4,'-');  //这里是后面两组的唯一值 如5770-A529AD987M
        $a4 = $a4.$this->GetRandStr(1);
        $a1 = $this->GetRandStr(8);
        $a2 = $this->GetRandStr(4);
        $a3 = $this->GetRandStr(4);
        $val = $a1.'-'.$a2.'-'.$a3.'-'.$a4;
        return strtoupper($val);
    }
    /**
     * 指定位置插入字符串
     * @param $str  原字符串
     * @param $i    插入位置
     * @param $substr 插入字符串
     * @return string 处理后的字符串
     */
    public function insertToStr($str, $i, $substr){
        $startstr="";
        for($j=0; $j<$i; $j++){
            $startstr .= $str[$j];
        }
        //指定插入位置后的字符串
        $laststr="";
        for ($j=$i; $j<strlen($str); $j++){
            $laststr .= $str[$j];
        }
        //将插入位置前，要插入的，插入位置后三个字符串拼接起来
        $str = $startstr . $substr . $laststr;
        //返回结果
        return $str;
    }
    /**
     * 获得指定位数随机数
     * @param $length  指定位数
     * @return string  处理后的字符串
     */
    public function GetRandStr($length){
        $str='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $len=strlen($str)-1;
        $randstr='';
        for($i=0;$i<$length;$i++){
            $num=mt_rand(0,$len);
            $randstr .= $str[$num];
        }
        return $randstr;
    }
}